<?php

namespace backend\controllers;

use backend\models\EduEnter;
use backend\models\EduEnterGifts;
use backend\models\EduMenu;
use backend\models\EduVideoCover;
use backend\models\MallMaterial;
use backend\models\MallMaterialMenu;
use backend\models\MallProduct;
use backend\models\MallProductSpecs;
use backend\models\MallPublisher;
use backend\models\MallVideo;
use Yii;
use yii\web\Controller;
use backend\models\Good;
use yii\web\Response;
use yii\web\UploadedFile;

class UploadController extends Controller
{
    /**
     * @return array|bool
     * 上传good图片
     */
    public function actionGood()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new Good();
        $model->scenario = 'upload';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'file');
            if ($fileTem) {
                $fileName =time();
                $savePath = 'uploads/' . $fileName . '.' . $fileTem->extension;
                $ret = $fileTem->saveAs($savePath);
                if ($ret) {
                    return ['code'=>true,'message'=>$savePath];
                } else {
                    return ['code'=>false,'message'=>$ret];
                }
            }
        }
        return false;
    }

    /**
     * @return array|bool
     * 上传edu_menu中icon
     */
    public function actionMenu()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new EduMenu();
        $model->scenario = 'upload';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'file');
            if ($fileTem) {
//                $fileName =time();
//                $savePath = 'icon/' . $fileName . '.' . $fileTem->extension;
//                $ret = $fileTem->saveAs($savePath);
                $savePath = "image/admin/menu/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4)];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

    public function actionMenuSelect()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new EduMenu();
        $model->scenario = 'upload-post';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'files');
            if ($fileTem) {
                $savePath = "image/admin/menu/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4)];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

    /**
     * @return array|bool
     * 上传视频封面
     */
    public function actionCover()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new EduVideoCover;
        $model->scenario = 'upload';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'file');
            if ($fileTem) {
//                $fileName =time();
//                $savePath = 'cover/' . $fileName . '.' . $fileTem->extension;
//                $ret = $fileTem->saveAs($savePath);
                $savePath = "image/admin/cover/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
//                var_dump($fileTem);
//                exit;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4)];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

    /**
     * @return array|bool
     * 上传进驻海报
     */
    public function actionEnter()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new EduEnter();
        $model->scenario = 'upload';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'file');
            if ($fileTem) {
//                $fileName =time();
//                $savePath = 'enter/' . $fileName . '.' . $fileTem->extension;
//                $ret = $fileTem->saveAs($savePath);
                $savePath = "image/admin/enter/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4)];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

    /**
     * @return array|bool
     * 礼包封面
     */
    public function actionEnterCover()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new EduEnterGifts();
        $model->scenario = 'upload';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'file');
            if ($fileTem) {
                $savePath = "image/admin/enter/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4)];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

    /**
     * @return array|bool
     * 礼包封面
     */
    public function actionEnterCoverPost()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new EduEnterGifts();
        $model->scenario = 'upload-post';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'files');
            if ($fileTem) {
                $savePath = "image/admin/enter/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4)];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

    /**
     * 上传商城中视频的封面图
     * @return array|bool
     */
    public function actionMallImg()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new MallVideo();
        $model->scenario = 'upload';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'file');
            if ($fileTem) {
                $savePath = "image/admin/mall/img/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4)];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

    /**
     * 上传商城的视频教学
     * @return array|bool
     */
    public function actionMallVideo()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new MallVideo();
        $model->scenario = 'upload-post';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'files');
            if ($fileTem) {
                $savePath = "video/admin/mall/video/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4)];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

    /**
     * 上传商城封面图
     * @return array|bool
     */
    public function actionMallProductImg()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new MallProduct();
        $model->scenario = 'upload';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'file');
            if ($fileTem) {
                $savePath = "image/admin/malls/img/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4)];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

    /**
     * 上传产品展示图
     * @return array|bool
     */
    public function actionMallProductImgs()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new MallProduct();
        $model->scenario = 'upload-post';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'files');
            if ($fileTem) {
                $savePath = "image/admin/malls/imgs/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4) . ','];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

    /**
     * 上传产品详情图
     * @return array|bool
     */
    public function actionMallProductDetail()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new MallProduct();
        $model->scenario = 'upload-detail';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'file_detail');
            if ($fileTem) {
                $savePath = "image/admin/malls/detail/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4) . ','];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

    /**
     * 上传产品视频
     * @return array|bool
     */
    public function actionMallProductVideo()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new MallProduct();
        $model->scenario = 'upload-video';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'file_video');
            if ($fileTem) {
                $savePath = "image/admin/malls/video/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4)];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

    /**
     * 上传产品规格图片
     * @return array|bool
     */
    public function actionMallProductSpecs()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new MallProductSpecs();
        $model->scenario = 'upload';
        if (Yii::$app->request->isPost) {
            $num = key($_FILES['MallProductSpecs']['tmp_name']);
            $fileTem = $model->file = UploadedFile::getInstance($model, "[{$num}]file");
            if ($fileTem) {
                $savePath = "image/admin/malls/specs/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4), 'num'=>$num];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

    /**
     * 上传素材图片
     * @return array|bool
     */
    public function actionMaterialImg()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new MallMaterial();
        $model->scenario = 'upload';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'file');
            if ($fileTem) {
                $savePath = "image/admin/material/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4)];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

    /**
     * 上传视频素材
     * @return array|bool
     */
    public function actionMaterialVideo()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new MallMaterial();
        $model->scenario = 'video';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'file_video');
            if ($fileTem) {
                $savePath = "video/admin/material/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4)];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

    /**
     * 发布者头像
     * @return array|bool
     */
    public function actionPublisher()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new MallPublisher();
        $model->scenario = 'upload';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'file');
            if ($fileTem) {
                $savePath = "image/admin/publisher/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4)];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

    public function actionMaterialMenu()
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $model = new MallMaterialMenu();
        $model->scenario = 'upload';
        if (Yii::$app->request->isPost) {
            $fileTem = $model->file = UploadedFile::getInstance($model, 'file');
            if ($fileTem) {
                $savePath = "image/admin/material/cover/".date("Ymd").time().str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$fileTem->extension;
                $ret = Yii::$app->QCloud->upload($savePath, $fileTem->tempName);
                if ($ret) {
                    return ['code'=>true,'message'=>'https'.substr(str_replace("%2F", "/", $ret['ObjectURL']),4)];
                } else {
                    return ['code'=>false,'message'=>json_encode($ret)];
                }
            }
        }
        return false;
    }

}